/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets;

import business.*;
import business.Empleado;
import business.consultasBD;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Query;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Alberto
 */
@WebServlet(name = "Login", urlPatterns = {"/Login"})
public class Login extends BaseServlet {



    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String usu = request.getParameter("username");
            String pass = request.getParameter("password");
            Alumno sacarAlumno = null;
            Empleado sacarEmpleado = null;
            preparaBD();
            consultasBD cdb=new consultasBD(jdbcUAO);
            sacarAlumno = cdb.getAlumnoByCorreo(usu);
            if (sacarAlumno!=null && sacarAlumno.getContrasena().equals(pass)) {
                HttpSession s = request.getSession();
                s.setAttribute("alumno", sacarAlumno);
            }
            
            RequestDispatcher r = null;
            
            if (sacarAlumno!=null && sacarAlumno.getContrasena().equals(pass)) {
                r = getServletContext().getRequestDispatcher("/header.jsp");
                r.include(request, response);
                r = getServletContext().getRequestDispatcher("/imprimiralumno.jsp");
                r.include(request, response);
                //out.println(sacarAlumno.getContrasena()+" "+pass);
            } else{
                sacarEmpleado = cdb.getEmpleadoByCorreo(usu);
                
                if (sacarEmpleado!=null){
                    if(pass!= null && sacarEmpleado.getContrasena().equals(pass)) {
                        HttpSession s = request.getSession();
                        s.setAttribute("empleado", sacarEmpleado);
                        r = null;
                        if (sacarEmpleado!=null && sacarEmpleado.getContrasena().equals(pass)) {
                            if(((Rol)sacarEmpleado.getRol()).getCaracteristicas().equals("propietario")){
                                r = getServletContext().getRequestDispatcher("/headerpropietario.jsp");
                                r.include(request, response);

                            }
                            if(((Rol)sacarEmpleado.getRol()).getCaracteristicas().equals("administrador")){
                                r = null;
                                r = getServletContext().getRequestDispatcher("/headeradministrador.jsp");
                                r.include(request, response);
                            }
                            r = null;
                            r = getServletContext().getRequestDispatcher("/imprimirempleado.jsp");
                            r.include(request, response);
                            //out.println(sacarAlumno.getContrasena()+" "+pass);
                        }
                    }
                }else {
                    r = getServletContext().getRequestDispatcher("/invalidLogin.jsp");
                    r.include(request, response);
                }
                

                
            }
            commitBD();
            r = getServletContext().getRequestDispatcher("/footer.jsp");
            r.include(request, response);
            /** TODO
             * - Comprobar si "usu" con "pass" esta en alumnos (select * from alumnos where nombre=usu and contraseña=pass
             *      - si existe hacer un include de alumno.jsp
             *      - Pegar en la sesion el Alumno resultante
             * - Comprobar si "usu" con "pass" esta en empleados (select * from empleados where nombre=usu and contraseña=pass
             *      - si existe hacer un include de empleado.jsp
             *      - Pegar en la sesion el Empleado resultante
             * - Si no existen en las anteriores: invalidLogin.jsp
             */
        }finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
